################################################################################
##
## Purpose: This script creates Figure 5
##
## Author: James Bisbee (james.h.bisbee@vanderbilt.edu)
##
## Input Files:
##  - ./data/prepped/finalData.RData: Prepped data from 9_DATA_final_build.R
##
## Output Files:
##  - ./output/figures/MS_figure_5.pdf
##
##
## See associated log file for compute environment, package versions, 
##  and date of most recent run.
##
################################################################################
rm(list = ls())
gc()
require(tidyverse)
require(ggridges)
require(fixest)

set.seed(123)

# Compute details
print(paste0('Compute environment from ',Sys.Date(),' run by Bisbee'))
if(Sys.info()['sysname'] == 'Windows') {
  ram_size = system("wmic MemoryChip get Capacity", intern = TRUE)[-1]
  model_name = system("wmic cpu get name", intern = TRUE)[2] # nocov
  vendor_id = system("wmic cpu get manufacturer", intern = TRUE)[2] # nocov
  
  print(list(ram = stringr::str_squish(ram_size)[1],
             vendor_id = stringr::str_squish(vendor_id),
             model_name = stringr::str_squish(model_name),
             no_of_cores = parallel::detectCores()))
} else if(Sys.info()['sysname'] == 'Linuxs') {
  splitted <- strsplit(system("ps -C rsession -o %cpu,%mem,pid,cmd", intern = TRUE), " ")
  df <- do.call(rbind, lapply(splitted[-1], 
                              function(x) data.frame(
                                cpu = as.numeric(x[2]),
                                mem = as.numeric(x[4]),
                                pid = as.numeric(x[5]),
                                cmd = paste(x[-c(1:5)], collapse = " "))))
  df
} else {
  cat("If not on Linux or Windows, you'll have to figure out your own solution to seeing the compute environment.")
}

sessionInfo()


load('./data/prepped/finalData.RData')

# data prep
keeps <- utterance_level %>%
  filter(grepl('FED',respondingTo)) %>%
  filter(!grepl('FED',opensecretsID)) %>%
  group_by(opensecretsID,respondingTo) %>%
  summarise(denom = n(),
            interrupted = sum(interrupted,na.rm=T)/all,
            interruptor = sum(interruptor,na.rm=T)/all) %>%
  ungroup() %>%
  distinct() %>%
  group_by(opensecretsID) %>%
  mutate(n=n()) %>%
  filter(n > 1) %>%
  filter(denom >= 20,
         opensecretsID != respondingTo)

toplot <- utterance_level %>%
  filter(opensecretsID %in% (keeps %>% filter(grepl('YELLEN',respondingTo)) %>% .$opensecretsID),
         ind > mind) %>%
  group_by(opensecretsID,party,gender,name,respondingTo,stab) %>%
  summarise(denom = n(),
            interrupted = sum(interrupted,na.rm=T),
            interruptor = sum(interruptor,na.rm=T),
            interruptedPct = interrupted*100/all,
            interruptorPct = interruptor*100/all) %>%
  ungroup() %>%
  filter(grepl('FED',respondingTo)) %>%
  mutate(id = paste0(str_to_title(name),' [',party,'-',stab,' (',gender,')]')) %>%
  distinct()


pdf('./output/figures/MS_figure_5.pdf',width = 7,height = 7)
toplot %>%
  mutate(id = factor(id,levels = toplot %>% filter(grepl('YELLEN',respondingTo)) %>%
                       arrange(interruptorPct) %>% .$id),
         Interrupting = ifelse(respondingTo == 'FEDYELLEN','Yellen','Male Fed Chairs')) %>%
  filter(!is.na(id)) %>%
  ggplot(aes(x = interruptorPct,y = id,fill = Interrupting,size = denom,shape = Interrupting)) + 
  geom_point(alpha = .6) + 
  theme_ridges() + 
  scale_size_continuous(name = '# Interactions') + 
  scale_shape_manual(values = c(21,22,24,25)) + 
  xlab('% Interrupting out of Total Utterances') + ylab('') + 
  guides(shape = guide_legend(override.aes = list(size = 4))) + 
  theme(axis.text.y = element_text(size = 8))
dev.off()

# EOF